package com.auxgroup.hr.front.service.process.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.enums.WfStatusEnum;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCWfCerHdr;
import com.auxgroup.hr.front.dao.domain.PsCWfEmpVw;
import com.auxgroup.hr.front.dao.repository.PsCWfCerHdrRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfEmpVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.process.WfCerHdrService;

/** 
 * @author wangchangyun
 * @version 创建时间：180601 
 * 社保公积金service
 */
@Service("wfCerHdrService")
public class WfCerHdrServiceImpl implements WfCerHdrService {
	private static final Logger logger = LoggerFactory.getLogger(WfCerHdrServiceImpl.class);
	
	@Autowired
	private PsCWfCerHdrRepository PsCWfCerHdrRepository;
	
	@Autowired
	private PsCWfEmpVwRepository psCWfEmpVwRepository;
	
	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;


	@Override
	public PsCWfCerHdr getWfCerHdr(String cTransactionNbr) {
		return PsCWfCerHdrRepository.findOne(cTransactionNbr);
	}


	@Override
	public PsCWfCerHdr save(PsCWfCerHdr wfCerHdr,UserDTO user) {
		logger.info("save PsCWfCerHdr start:",wfCerHdr);
		JobCurrPrimary primary = new JobCurrPrimary();
		primary.setEmplid(user.getEmplId());
		primary.setEmplRcd(new BigDecimal(0));
		PsCWfEmpVw emp = psCWfEmpVwRepository.findOne(primary);//获取个人信息内容
		wfCerHdr.setCreatedttm(new Timestamp(System.currentTimeMillis())); // 创建时间
		wfCerHdr.setLastupddttm(new Timestamp(System.currentTimeMillis())); // 更新时间
		wfCerHdr.setcWfStatus(WfStatusEnum.WF_STATUS_S.getStatus()); // 流程状态
		wfCerHdr.setcWfType(WfTypeEnum.WF_STYPE_CER.getType());// 流程类别
		if(StringUtils.isBlank(wfCerHdr.getAddress100())){
			wfCerHdr.setAddress100("自提");
		}
		wfCerHdr.setCreatedBy(user.getOprId()); // 创建人用户
		wfCerHdr.setLastupdoprid(user.getOprId()); // 更新者
		objectMapper.map(emp, wfCerHdr);
		ObjectFieldUtils.handleNullValue(wfCerHdr);//过滤空值
		return PsCWfCerHdrRepository.save(wfCerHdr);
	}
	
}
